package com.liu.SRMan.web;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.g4studio.core.json.JsonHelper;
import org.g4studio.core.metatype.Dto;
import org.g4studio.core.metatype.impl.BaseDto;
import org.g4studio.core.mvc.xstruts.action.ActionForm;
import org.g4studio.core.mvc.xstruts.action.ActionForward;
import org.g4studio.core.mvc.xstruts.action.ActionMapping;
import org.g4studio.core.mvc.xstruts.actions.BaseAction;
import org.g4studio.core.mvc.xstruts.upload.FormFile;
import org.g4studio.core.orm.xibatis.sqlmap.client.SqlMapClient;
import org.g4studio.core.orm.xibatis.support.SqlMapClientTemplate;
import org.g4studio.core.util.G4Constants;
import org.g4studio.core.util.G4Utils;
import org.g4studio.core.web.BizAction;
import org.g4studio.core.web.CommonActionForm;
import org.g4studio.system.common.util.idgenerator.IDHelper;

import com.liu.SRMan.service.HorizontalService;
import com.liu.SRMan.service.LongitudinalService;

public class LongitudinalAction extends BizAction {

	private LongitudinalService longitudinalService = (LongitudinalService) getService("longitudinalService");
	/*
	 * 进入新建纵向项目计划页面用action
	 */
	public ActionForward newApplyPlan(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("newApplyPlan");
	}
	
	/*
	 * 进入新建纵向项目页面用action
	 */
	public ActionForward newLongitudinalProject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("newLongitudinalProject");
	}
	
	/*
	 * 进入纵向中检计划列表页面用action
	 */
	public ActionForward midInspectPlan(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response)throws Exception {

		return mapping.findForward("midInspectPlan");
	}
	
	/*
	 * 进入纵向学校初审页面用action
	 */
	public ActionForward schoolAudit(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		
		return mapping.findForward("schoolAudit");
	}
	/*
	 * 进入纵向项目计划列表页面用action
	 */
	public ActionForward overView(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("overView");
	}
	
	/*
	 * 进入纵向项目结项页面用action
	 */
	public ActionForward closeProject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("closeProject");
	}
	
	/*
	 * 保存纵向项目计划用action
	 */
	public ActionForward savePlan(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);

		Dto outDto = longitudinalService.saveApplyPlan(inDto);

		outDto.put("success", new Boolean(true));
		outDto.put("msg", "横向项目数据保存成功!");
		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}

	/*
	 * 保存纵向项目用action
	 */
	public ActionForward saveLongitudinalProject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		
		Dto inDto = aForm.getParamAsDto(request);
		FormFile myFile = aForm.getFile1();
		String savePath = getServlet().getServletContext().getRealPath("\\") + "uploaddata\\longitudinal\\";
		String user_id = getSessionContainer(request).getUserInfo().getUserid();
		
		Dto outDto = longitudinalService.newLongitudinal(inDto, myFile, user_id, savePath);
		
		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}
	
	/*
	 * 修改纵向项目用action
	 */
	public ActionForward updateLongitudinalProject(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm cForm = (CommonActionForm) form;
		
		Dto inDto = cForm.getParamAsDto(request);
		FormFile myFile = cForm.getFile1();
		String savePath = getServlet().getServletContext().getRealPath("\\") + "uploaddata\\longitudinal\\";
		String user_id = getSessionContainer(request).getUserInfo().getUserid();
		
		Dto outDto = longitudinalService.updateLongitudinal(inDto, myFile, user_id, savePath);
		
		System.out.println("接收到的表单提交参数：\n" + inDto);
		write(JsonHelper.encodeObject2Json(outDto), response);
		setOkTipMsg("数据修改成功", response);
		return mapping.findForward(null);
	}

	/*
	 * 查询纵向项目填充页面combobox用action
	 */
	public ActionForward queryProjectDatas(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List areaList = g4Reader.queryForList("Temp.getProjectData", dto);
		String jsonString = JsonHelper.encodeObject2Json(areaList);
		write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/*
	 * 根据项目ID，查询项目信息
	 */
	public ActionForward getLproject(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			CommonActionForm cForm = (CommonActionForm) form;
			Dto inDto = cForm.getParamAsDto(request);
			Dto outDto = (BaseDto)g4Reader.queryForObject("Temp.getLProjectById", inDto);
			if (G4Utils.isEmpty(outDto)) {
				outDto = new BaseDto();
				outDto.put("msg", "没有查询到数据");
			}else {
				outDto.put("msg", "ok");
			}
			String jsonString = JsonHelper.encodeDto2FormLoadJson(outDto, G4Constants.FORMAT_Date);
			write(jsonString, response);
			return mapping.findForward(null);
	}
	
	/*
	 * 纵向项目计划检索用action
	 */
	public ActionForward queryApplyPlan(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		List list = g4Reader.queryForPage("Temp.queryApplyPlan", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countApplyPlan", dto);
		System.out.println("接收到的表单提交参数：\n" + dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/*
	 * 中检计划检索用action
	 */
	public ActionForward queryMidInspectPlan(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		List list = g4Reader.queryForPage("Temp.queryInspectPlan", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("Temp.countInspectPlan", dto);
		System.out.println("接收到的表单提交参数：\n" + list);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/*
	 * 纵向项目检索用action
	 */
	public ActionForward queryLongitudinalInfo(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		List list = g4Reader.queryForPage("Temp.queryLongitudinal", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countLongitudinal", dto);
		System.out.println("接收到的表单提交参数：\n" + dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	/*
	 * 检索结项的纵向项目用action
	 */
	public ActionForward queryFinishLongitudinal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		List list = g4Reader.queryForPage("Temp.queryFinishLongitudinal", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countFinishLongitudinal", dto);
		System.out.println("接收到的表单提交参数：\n" + dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/*
	 * 检索学校初审的纵向项目用action
	 */
	public ActionForward queryAuditLongitudinal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		List list = g4Reader.queryForPage("Temp.queryAuditLongitudinal", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countAuditLongitudinal", dto);
		System.out.println("接收到的表单提交参数：\n" + dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		
		return mapping.findForward(null);
	}
	
	/*
	 * 结项纵向项目用action
	 */
	public ActionForward finishLongitudinal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		System.out.println("接收到的表单提交参数：\n" + dto);

		longitudinalService.finishLongitudinal(dto);

		setOkTipMsg("项目结项成功", response);
		return mapping.findForward(null);
	}
	
	/*
	 * 初审纵向项目用action
	 */
	public ActionForward auditLongitudinal(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		System.out.println("接收到的表单提交参数：\n" + dto);

		longitudinalService.auditLongitudinal(dto);

		setOkTipMsg("完成项目初审", response);
		return mapping.findForward(null);
	}
	
	/*
	 * 检索项目参与人员用action
	 */
	public ActionForward queryMembers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);

		System.out.println("接收到的表单提交参数：\n" + dto);

		List list = g4Reader.queryForList("Temp.queryProjectMembers", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"Temp.countProjectMembers", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				null);

		super.write(jsonString, response);
		return mapping.findForward(null);
	}
	
	/*
	 * 保存中检计划用action
	 */
	public ActionForward saveInspectPlan(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		
		FormFile myFile = aForm.getFile1();
		String savePath = getServlet().getServletContext().getRealPath("\\") + "uploaddata\\longitudinal\\";
		String user_id = getSessionContainer(request).getUserInfo().getUserid();
		
		Dto outDto = longitudinalService.newInspectPlan(inDto,myFile,user_id,savePath);

		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}
	
	/*
	 * 删除纵向项目用action
	 */
	public ActionForward deleteLongitudinalProject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		Dto temp = (BaseDto)g4Reader.queryForObject("Temp.getLProjectById", inDto);
		String filePath = temp.getAsString("attachment");
		String savePath = getServlet().getServletContext().getRealPath("\\") + "uploaddata\\longitudinal\\";
		
		Dto outDto = longitudinalService.deleteLongitudinal(inDto,filePath,savePath);
		
		write(JsonHelper.encodeObject2Json(outDto), response);
		return mapping.findForward(null);
	}
	
	
}